<?php

namespace Modules\ShopAdmin\Models;
use Illuminate\Support\Facades\DB;  
use Modules\ShopAdmin\Models\ShopProductSpec as ShopProductSpecModel; 
class ShopProduct extends BaseShopModel
{	
	/**
     * @name  状态
     */
	public static function product_status(){
		return [
			'0'=>'下架',
			'1'=>'上架'
		];
	}
	/**
     * @name  是否精品推荐
     */
	public static function recommend_status(){
		return [
			'0'=>'否', 
			'1'=>'是'
		];
	}
	/**
     * @name  是否热卖单品
     */
	public static function best_status(){
		return [
			'0'=>'否',
			'1'=>'是'
		];
	}
	/**
     * @name  关联商品分类
     */
    public function product_type_to()
    {
        return $this->belongsTo('Modules\ShopAdmin\Models\ShopProductType','product_type_id','id');
    }
	/**
     * @name  关联sku
     */
    public function product_spec_to()
    {
        return $this->hasMany('Modules\ShopAdmin\Models\ShopProductSpec','product_id','id');
    }
	/**
     * @name  关联图片
     */
    public function image_one()
    {
        return $this->hasOne('Modules\Admin\Models\Image','id','image_id');
    }
	/**
     * @name  关联评论
     */
    public function comments()
    {
        return $this->hasOne('Modules\ShopAdmin\Models\ShopProductComment','product_id','id');
    }
	/**
     * @name  解析列表数据
     */
    public static function product_list($data)
    {
        foreach($data as $k=>$v){
			$data[$k]['type_title'] = $v['product_type_to']['title'];
			$data[$k]['image'] = $v['image_one']['url'];
			$data[$k]['stock'] = array_sum(array_column($v['product_spec_to'],'stock'));
		}
		return $data;
    }
	/**
     * @name  商品添加
     */
	public function product_add($data,$url){
		$specs_data = $data['specs_data'];
		$product_data = [
			'product_type_id'  	=> $data['product_type_id'],
			'name'				=> $data['name'],
			'image_id'			=> $data['image_id'],
			'attrbute_list'		=> $data['specs_status']?json_encode($data['attrbute_list'],JSON_UNESCAPED_UNICODE):'',
			'content'			=> isset($data['content'])?$data['content']:'',
			'specs_status'		=> $data['specs_status'],
			'created_at' 		=> date('Y-m-d H:i:s'),
			'images'			=> isset($data['images'])?$data['images']:'',
			'sales'             => array_sum(array_column($specs_data,'sales')),
			'money'             => $specs_data[0]['money'],
			'bazaar_money'      => $specs_data[0]['bazaar_money'],
			'postage'      		=> $specs_data[0]['postage'],
		];
		DB::beginTransaction();
		try{
			$id = $this->insertGetId($product_data);
			foreach($specs_data as $k=>$v){
				$specs_data[$k]['product_id'] = $id; 
				$specs_data[$k]['specs'] = $data['specs_status']?json_encode($v['specs'],JSON_UNESCAPED_UNICODE):''; 
				$specs_data[$k]['created_at'] = date('Y-m-d H:i:s');
			}  
			DB::table('shop_product_specs')->insert($specs_data);	
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return response()->json([
				'status'=>0,
				'error' => config('admin.create_error'),
				'msg' => '',
				'url'=>$url
			],201);
		}
		return response()->json([
			'status'=>1,
			'error' => '',
			'msg' => config('admin.create_success'),
			'url'=>$url
		],200);
	}
	/**
     * @name  删除商品（后续添加商品限制）
     */
	public function product_destroy($id,$url){
		$ShopProductSpecModel = new ShopProductSpecModel();
		$arr = $ShopProductSpecModel->where(['product_id'=>$id])->get()->toArray();
		$arr = array_column($arr,'id');
		DB::beginTransaction();
		try{
			$this->destroy($id);
			$ShopProductSpecModel->destroy($arr);	
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return response()->json([
				'status'=>0,
				'error' => $error,
				'msg' => '',
				'url'=>config('admin.delete_error') 
			],201);
		}
		return response()->json([
			'status'=>1,
			'error' => '',
			'msg' => config('admin.delete_success'),
			'url'=>$url
		],200);
	}  
	
	/**
     * @name  数据编辑
     */
    public function product_update($data,$id,$url)
    {
		$product_data = [
				'content'		=> $data['content'],
				'image_id'		=> $data['image_id'],
				'name'			=> $data['name'],
				'sort'			=> $data['sort'],
				'updated_at'	=> date('Y-m-d H:i:s'),
				'images'		=> isset($data['images'])?$data['images']:'',
				'sales'         => array_sum(array_column($data['specs_data'],'sales')),
				'money'         => $data['specs_data'][0]['money'],
				'bazaar_money'  => $data['specs_data'][0]['bazaar_money'],
				'postage'       => $data['specs_data'][0]['postage'],
			]; 	 
		$ShopProductSpecModel = new ShopProductSpecModel();	
		DB::beginTransaction();
		try{
			$this->where('id',$id)->update($product_data);
			foreach($data['specs_data'] as $k=>$v){
				$ShopProductSpecModel->where('id',$v['id'])->update([
						'money'			=>$v['money'],
						'bazaar_money'	=>$v['bazaar_money'],
						'cost_money'	=>$v['cost_money'],
						'postage'		=>$v['postage'],
						'stock'			=>$v['stock'],
						'sales'			=>$v['sales'],
				]);
			}
			
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return response()->json([
				'status'=>0,
				'error' => config('admin.update_error'),
				'msg' => '',
				'url'=>$url
			],201);
		}
		return response()->json([
			'status'=>1,
			'error' => '',
			'msg' => config('admin.update_success'),
			'url'=>$url
		],200);	 
    }
	
	
	
	
}
